import { Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { NewsArticlesEntity } from '../entity/articles';
import { NewsCategoriesEntity } from '../entity/categories';

/**
 * 描述
 */
@Provide()
export class NewsArticlesService extends BaseService {
  @InjectEntityModel(NewsArticlesEntity)
  NewsArticlesEntity: Repository<NewsArticlesEntity>;
  @InjectEntityModel(NewsCategoriesEntity)
  NewsCategoriesEntity: Repository<NewsCategoriesEntity>;
  /**
   * 重载新增接口
   */
  async add(param) {
    console.log('~~~~~serive被调用了');
    console.log(param);
    const { category_id } = param;
    if (category_id) {
      const categories = await this.NewsCategoriesEntity.findOne(category_id);
      console.log(categories);
      categories.article_count = categories.article_count + 1;
      await this.NewsCategoriesEntity.save(categories);
    }

    return await super.add(param);
  }
}
